#include "TestTabForm.h"
#include <CommCtrl.h>
#include <Zaf\String\ToString.h>


#define ALIGNMENT_TOP 0x0001
#define ALIGNMENT_BOTTOM 0x002
#define ALIGNMENT_LEFT 0x004
#define ALIGNMENT_RIGHT 0x008



struct ControlInfo {

	WORD Id;
	const wchar_t* Text;

} g_alignmentButtonInfo[] = {

	ALIGNMENT_TOP, L"Top",
	ALIGNMENT_BOTTOM, L"Bottom",
	ALIGNMENT_LEFT, L"Left",
	ALIGNMENT_RIGHT, L"Right",
};


TestTabPage::TestTabPage() {

}


void TestTabPage::SetTab(Tab& tab) {
	m_tab = &tab;
}


LRESULT TestTabPage::WndProc(const WndMsg& msg) {

	switch (msg.Message) {

	case WM_COMMAND: {	

		if (msg.WParamHigh == BN_CLICKED) {

			switch (msg.WParamLow) {

			case 1:
				m_tab->SetAppearance(TabAppearance::Tabs);
				break;

			case 2:
				m_tab->SetAppearance(TabAppearance::Buttons);
				break;

			case 3:
				m_tab->SetAppearance(TabAppearance::FlatButtons);
				break;
			}

			return 0;
		}
		break;
					 }
	}

	return TabPage::WndProc(msg);
}



TestTabForm::TestTabForm() {

	this->CreatedEvent.Register(this, &TestTabForm::OnCreated);

	m_tab.SelectedPageChangingEvent.Register(this, &TestTabForm::OnSelectedChanging);
	m_tab.SelectedPageChangedEvent.Register(this, &TestTabForm::OnSelectedChanged);
}



void TestTabForm::OnCreated(CreatedEventArgs& args) {

	this->SetSize(500, 400);

	m_tab.SetSize(300, 300);
	m_tab.SetPosition(0, 0);
	m_tab.SetParent(*this);
	m_tab.Create();

	for (int index = 0; index != 3; ++index) {

		m_pages[index].SetText(Zaf::String::ToString<wchar_t>(index).c_str());
		m_pages[index].SetParent(*this);
		m_pages[index].SetTab(m_tab);
		m_pages[index].HasBorder(true);
		m_pages[index].Create();
		
		m_tab.InsertPage(m_pages[index]);
	}

	for (int index = 0; index != 3; ++index) {

		m_buttons[index].SetID(index + 1);
		m_buttons[index].SetText(Zaf::String::ToString<wchar_t>(index).c_str());
		m_buttons[index].SetParent(m_pages[index]);
		m_buttons[index].SetSize(100, 30);
		m_buttons[index].SetPosition(10 + index, 10 + index);
		m_buttons[index].Create();
	}

	unsigned int x = 0;

	for (int index = 0; index != AlignmentButtonCount; ++index) {

		m_alignmentButtons[index].SetID(g_alignmentButtonInfo[index].Id);
		m_alignmentButtons[index].SetText(g_alignmentButtonInfo[index].Text);
		m_alignmentButtons[index].SetParent(*this);
		m_alignmentButtons[index].SetSize(100, 30);
		m_alignmentButtons[index].SetPosition(x, 320);
		m_alignmentButtons[index].Create();

		x += 100;
	}
}



void TestTabForm::OnSelectedChanging(SelectedPageChangingArgs& args) {

}



void TestTabForm::OnSelectedChanged(SelectedPageChangedArgs&) {
	
}



LRESULT TestTabForm::WndProc(const WndMsg& msg) {

	switch (msg.Message) {

	case WM_COMMAND:
		if (msg.WParamHigh == BN_CLICKED) {
			
			switch (msg.WParamLow) {

			case ALIGNMENT_TOP:
				m_tab.SetAlignment(TabAlignment::Top);
				return 0;

			case ALIGNMENT_BOTTOM:
				m_tab.SetAlignment(TabAlignment::Bottom);
				return 0;

			case ALIGNMENT_LEFT:
				m_tab.SetAlignment(TabAlignment::Left);
				return 0;

			case ALIGNMENT_RIGHT:
				m_tab.SetAlignment(TabAlignment::Right);
				return 0;
			}
		}
		break;
	}

	return Form::WndProc(msg);
}